'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AttendanceIndexCtrl
 * @description
 * # AttendanceIndexCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AttendanceIndexCtrl', function ($location, DataService, openApi, localStorageService, MapService, Util) {
        var vm = this;
        this.checkinfo = {}
        this.checkinfo.today = new Date;
        this.goAttendancelist = function () {
            $location.path("attendance/attendance_list")
        }
        // 上班打卡
        this.checksign = function (type) {
            if (vm.checkinfo.signType != type) {
                return
            }
            this.signed = !this.signed;
            var param = {};
            if (typeof device != "undefined") {
                param.deviceToken = device.uuid;
            }
            param.acId = vm.checkinfo.acId;
            param.overTime = vm.checkinfo.overTime;
            param.signType = vm.checkinfo.signType;
            //  0-范围外 1-正常 2-迟到 3-早退 默认传1，当不在打卡距离内需要传值0 并输入理由
            param.logType = 1;
            MapService.getcurPositon().then(function (data) {
                var curPoint = data;
                var attPoint = {};
                var ifPermission;
                attPoint.latitude = vm.checkinfo.lat;
                attPoint.longitude = vm.checkinfo.lng;
                param.distance = MapService.getDistance(attPoint, curPoint);
                ifPermission = MapService.permissionDistance(attPoint, curPoint, vm.checkinfo.offSet);
                // 是否到了可以打卡的地点
                if (ifPermission) {
                    param.lat = curPoint.latitude;
                    param.lng = curPoint.longitude;
                    param.address = curPoint.address;
                    // 根据经纬度获取地址
                    // 打卡
                    DataService.getData(openApi.punchClock, param).then(function (data) {
                        // 没有迟到
                        // 1：正常，说明打卡成功，此时result 为当前的打卡时间，直接更新
                        // -1：不正常，说明打卡不成功，此时提示result
                        // 2：说明迟到了，此时弹出输入框，输入迟到理由，再次提交的时候设置logType=2
                        // 3：说明早退了，此时弹出输入框，输入早退理由，再次提交的时候设置logType=3
                        if (data.code == -1) {
                            Util.Toast(data.result);
                        } else if (data.code == 1) {
                            vm.checkinfo = data.result;
                            navigator.notification.beep(1);
                            Util.Toast("打卡成功");
                            localStorageService.set("checkinfo", data.result);
                        } else if (data.code == 2) {
                            // 迟到，补充理由
                            param.logType = 2;
                            Util.saveLocalInfo("attParam", param);
                            $location.path("attendance/attendance_late/late");
                        } else if (data.code == 3) {
                            // 早退，补充理由
                            param.logType = 3;
                            Util.saveLocalInfo("attParam", param);
                            $location.path("attendance/attendance_late/early");
                        }
                    })

                } else {
                    // 外勤，去到外勤页面
                    param.logType = 0;
                    Util.saveLocalInfo("attParam", param);
                    $location.path("attendance/attendance_outside");
                }

            });

        };

        // 考勤统计页面
        this.goStatisticlist = function () {
            $location.path("attendance/attendance_statistic")
        };
        // 切换加班状态
        this.switchOvertime = function () {
            DataService.getData(openApi.changeOvertime).then(function (data) {
                vm.checkinfo = data;
            })
        };
        // 获取打卡信息
        var getCheckInfo = function () {
            DataService.getData(openApi.getAttendanceStatus).then(function (data) {
                vm.checkinfo = data;
                localStorageService.set("checkinfo", data);
            })
        }
        var init = function () {
            getCheckInfo();
        }
        init();
    });
